package com.atguigu.gulimall.order.listener;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@RabbitListener(queues = "order.release.order.queue")
@Service
public class OrderCloseListener {

    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void listener(OrderEntity entity, Channel channel, Message message) throws IOException {
        System.out.println("收到过期信息，准备关闭订单" + entity);

        try {
            //关闭订单
            orderService.closeOrder(entity);
            /**
             * 支付宝收单功能
             * 1、订单在支付页不支付，等到订单关闭之后支付，导致订单状态变为已支付，但库存解锁了
             * 2、由于时延问题，订单解锁完成，正在解锁库存时候，异步通知才到
             *      订单解锁时候手动调用收单
             * 3、其他问题
             *      每天晚上下载支付宝对账单，一一进行对账
             *
             */
            //TODO 手动调用收单
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
